﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade.entities;
using BLFacade.enums;

namespace ProjectUI
{
    public partial class TaskListForm : Form
    {
        private List<ITask> tasks;
        
        private IProject project;

        public TaskListForm(IProject project)
        {
            InitializeComponent();
            gridViewTasks.AutoGenerateColumns = false;
            this.project = project;
            tasks = project.Tasks;
            gridViewTasks.DataSource = tasks;
            this.Text = String.Format("Tasks in {0}", project.Name);
            fillChart();
        }

        public TaskListForm(IEmployee employee)
        {
            InitializeComponent();
            gridViewTasks.AutoGenerateColumns = false;
            tasks = employee.Tasks;
            gridViewTasks.DataSource = tasks;
            btnAdd.Visible = false;
            this.Text = String.Format("Tasks of {0}", employee.FullName);
            fillChart();
        }

        private void gridViewTasks_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = gridViewTasks.SelectedCells[0].RowIndex;
            TaskForm info = new TaskForm(((List<ITask>)gridViewTasks.DataSource)[index]);
            info.MdiParent = this.MdiParent;
            info.Show();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            TaskForm info = new TaskForm(project);
            info.MdiParent = this.MdiParent;
            info.Show();
            this.Close();
        }

        private void fillChart()
        {
            foreach (TaskStatus i in Enum.GetValues(typeof(TaskStatus)))
            {
                chrtTasks.Series[0].Points.AddXY(Enum.GetName(typeof(TaskStatus), i),
                    tasks.Where(t => i.Equals(t.Status)).Count());
                chrtTasks.Series[0].Points.Last()["PieLabelStyle"] = "Disabled";
            }

            var activeTasks = tasks.Where(t => t.Status.Equals(TaskStatus.InProgress) ||
                t.Status.Equals(TaskStatus.Opened) ||
                t.Status.Equals(TaskStatus.Reopened)).ToList();

            if (activeTasks.Count() == 0)
            {
                chrtPriorities.Visible = false;
            }
            else
            {
                foreach (TaskPriority i in Enum.GetValues(typeof(TaskPriority)))
                {
                    chrtPriorities.Series[0].Points.AddXY(Enum.GetName(typeof(TaskPriority), i),
                        activeTasks.Where(t => i.Equals(t.Priority)).Count());
                    chrtPriorities.Series[0].Points.Last()["PieLabelStyle"] = "Disabled";
                }
            }


        }
    }
}
